package com.qf.service.impl;

import com.qf.dao.UserDao;
import com.qf.dao.impl.UserDaoImpl;
import com.qf.domain.User;
import com.qf.service.UserService;
import com.qf.utils.MD5Utils;

import java.util.List;

public class UserServiceImpl implements UserService {
    private UserDao userDao=new UserDaoImpl();
    @Override
    public boolean checkUserName(String username) {
        User user=userDao.selectByUserName(username);
        if(user!=null){
            return true;
        }
        return false;
    }

    @Override
    public void register(User user) {
        user.setPassword(MD5Utils.md5(user.getPassword()));
        userDao.insert(user);
    }

    @Override
    public User login(String username, String password) {
        password=MD5Utils.md5(password);
        User user=userDao.selectByUserName(username);
        if (user == null) {
            throw new RuntimeException("用户名不存在");
        }
        if(!user.getPassword().equalsIgnoreCase(password)){
            throw new RuntimeException("密码错误");
        }
        if(user.getFlag()!=1){
            throw new RuntimeException("账户未激活或已过期");
        }
        return user;
    }

    @Override
    public List<User> findName(String use, String gen) {
        return userDao.selectaName(use,gen);
    }

    @Override
    public List<User> findUserInfo(String where, List<Object> params, boolean flag) {
        return userDao.selectAll(where,params,flag);
    }

    @Override
    public void removeId(int did) {
        userDao.deleteId(did);
    }

    @Override
    public User findById(Integer uid) {
        return userDao.selectById(uid);
    }
}
